#include "Application.h"

bool Application::addRectangle ()
{
	if (m_shapeCon->addRectangle ())
	{
		this->drawShapeCon ();
		m_shapeCon->setSaveStamp(false);
		return true;
	}

	return false;
}

bool Application::addCircle ()
{
	if (m_shapeCon->addCircle ())
	{
		this->drawShapeCon ();
		m_shapeCon->setSaveStamp(false);
		return true;
	}

	return false;
}

void Application::drawShapeCon () 
{
	m_ptrScrCur = &m_scrCur;
	m_shapeCon->drawToScreenWithoutSplit (m_ptrScrCur, m_defaultDraw);
	system("cls");
	m_scrCur.print();
	m_scrCur.clearScreen();
}

void Application::select ()
{
	Point p1;
	unsigned int index;
	p1.input();
	clrscr();
	if (!m_shapeCon->searchShape(p1 ,index))
	{
		this->drawShapeCon();
		p1.draw('@');
		cout << endl;
		gotoxy (0,21);
		cout << "Not found" << endl;
	}
	else
	{
		m_shapeCon->drawSpecific(m_defaultDraw, index , '+');
		p1.draw('@');
		gotoxy (0,23);
		cout << endl << "Press any key to continue...";	
		int temp = _getch();
		clrscr();
		SubMenu sub(m_shapeCon, index);
		sub.run();
	}
}

bool Application::delShape ( unsigned int index)
{
	if (m_shapeCon->shapeDelete(index))
	{
		m_shapeCon->setSaveStamp(false);
		return true;
	}
		
	return false;
}

bool Application::moveShape ( unsigned int index)
{
	int axisX, axisY;

	cout << "Please enter an axis for the x value : ";
	cin >> axisX;
	cout << "Please enter an axis for the y value : ";
	cin >> axisY;
	cout << endl;

	if (m_shapeCon->setAxis(index, axisX, axisY))
	{
		m_shapeCon->setSaveStamp(false);
		return true;
	}
		
	return false;
}

bool Application::addAnimationToShape ( unsigned int index)
{
	double dirX, dirY;

	cout << "Please enter the direction on X axis (between -1 to 1) : ";
	cin >> dirX;
	cout << "Please enter the direction on Y axis (between -1 to 1) : ";
	cin >> dirY;
	cout << endl;

	if (m_shapeCon->setDirection(index, dirX, dirY))
	{
		m_shapeCon->setSaveStamp(false);
		return true;
	}
		
	return false;
}

void Application::printAnimWithSplit()
{
	m_ptrScrCur = &m_scrCur;
	m_ptrScrNew = &m_scrNew;

	system("cls");
	while (!( _kbhit() && _getch() == 27))//ESC))
	{
		m_shapeCon->drawToScreen (m_ptrScrNew, m_defaultDraw);
		m_ptrScrCur->Diff (m_ptrScrNew);

		m_scrCur = m_scrNew;
		m_shapeCon->drawToScreen (m_ptrScrNew, m_defaultDraw, ' ');

		m_shapeCon->addTimeToAnim ();
		m_shapeCon->updatePrintWithSplit();
		
		Sleep(100);
	}
	m_shapeCon->initSum();
	m_scrNew.clearScreen();
	m_scrCur.clearScreen();
	m_shapeCon->initPrintWithSplit();
}

void Application::printAnimWithCollision()
{
	m_ptrScrCur = &m_scrCur;
	m_ptrScrNew = &m_scrNew;

	m_shapeCon->saveOriginalDir();

	// check first if there are some rectangles that shouldn't move
	m_shapeCon->checkStuckShape();
	
	system("cls");
	while (!( _kbhit() && _getch() == ESC))
	{
		// Checks the next step of the rectangles.
		// if there are some collisions, change directions accordingly
		m_shapeCon->drawToScreenWithCollision (m_ptrScrNew);

		// Print to screen only the differences between the older and newer screens
		m_ptrScrCur->Diff (m_ptrScrNew);

		// Switch between the current screen and the new screen -
		// the current screen in the next itteration is the "new" screen on the 
		// current itteration
		m_scrCur = m_scrNew;

		// Initialize the new screen - preperation for the next itteration
		m_shapeCon->drawToScreen (m_ptrScrNew, m_defaultDraw, ' ');

		m_shapeCon->addTimeToAnim ();

		//m_scrNew.clearScreen();
		//int moshe = _getch();
		Sleep(100);
	}
	m_shapeCon->restoreOriginalDir();
	m_shapeCon->initSum();
	m_scrNew.clearScreen();
	m_scrCur.clearScreen();
}

bool Application::saveTextFile(string & fileName) const
{
	bool overwrite = true;
	// is file empty?
	if (fileName == "")
	{
		cerr << "File name is empty..." << endl;
		return false;
	}
	// 
	fileName.append(".txt");

	ifstream myOtherFile (fileName);
	if (myOtherFile)
	{
		if (!checkOverwrite())
			overwrite = false;

	}
	myOtherFile.close();
	if (!overwrite)
		return false;

	ofstream myFile (fileName, ios::trunc );

	if (!m_shapeCon->writeToText (myFile))
	{
		cerr << "Failed to save to file" << endl;
		return false;
	}
	// Mark that all data has been saved
	m_shapeCon->setSaveStamp(true);

	// Close file
	myFile.close();

	return true;
}

bool Application::saveBinFile(string & fileName) const
{
	bool  overwrite = true;
	// is file empty?
	if (fileName == "")
	{
		cerr << "File name is empty..." << endl;
		return false;
	}
	// 
	fileName.append(".bin");

	ifstream myOtherFile (fileName);
	if (myOtherFile)
	{
		if (!checkOverwrite())
			overwrite = false;

	}
	myOtherFile.close();
	if (!overwrite)
		return false;


	ofstream myFile(fileName, ios::out|ios::binary|ios::trunc);

	if (!m_shapeCon->writeToBin (myFile))
	{
		cerr << "Failed to save to file" << endl;
		return false;
	}
	// Mark that all data has been saved
	m_shapeCon->setSaveStamp(true);

	// Close file
	myFile.close();

	return true;
}

bool Application::loadTextFile(string & fileName) const
{
	// is file empty?
	if (fileName == "")
	{
		cerr << "File name is empty..." << endl;
		return false;
	}
	// 
	fileName.append(".txt");

	ifstream myFile (fileName);

	if (!m_shapeCon->readFromText (myFile))
	{
		cerr << "Failed to load from file" << endl;
		return false;
	}
	// Mark that all data has been saved
	m_shapeCon->setSaveStamp(true);

	// Close file
	myFile.close();

	return true;
}

bool Application::loadBinFile(string & fileName) const
{
	// is file empty?
	if (fileName == "")
	{
		cerr << "File name is empty..." << endl;
		return false;
	}
	// 
	fileName.append(".bin");

	ifstream myFile (fileName);

	if (!m_shapeCon->readFromBin (myFile))
	{
		cerr << "Failed to load from file" << endl;
		return false;
	}
	// Mark that all data has been saved
	m_shapeCon->setSaveStamp(true);

	// Close file
	myFile.close();

	return true;
}

bool Application::checkOverwrite() const 
{
	char cont;

	// If the user is trying to save a file exists a message will pop
	cout << "You are about to overwrite a file "<< endl;
	do
	{
		cout << "Do you wish to continue? (y - yes, n - no)" << endl;
		cin >> cont;
		cont = tolower(cont);
		cout << endl;
	}
	while (cont != 'y' && cont != 'n');

	switch (cont)
	{
	case 'y':
		return true;
		break;

	case 'n':
		return false;
		break;
	}
	return false;

}
